<?php
/**
 * 
 * Author: wxl
 * Date: 2016/4/25
 * Time: 09:44
 * intro:充电宝充电 
 */

class UserService extends BaseService
{

    /**
     * 用户注册时，添加在userInfo表中的信息
     * @param $data
     * @param $userId
     */
    public function updateUserInfo($data,$userId) {
        if($data && $userId) {
            $res = M('UserInfo')->where(array('user_id' => $userId))->find();
            if(isset($data['school_id'])&&$data['school_id'] !=''){
                $schoolName = M(School)->where(array('school_id'=>$data['school_id']))->field('school_name')->find();
                $data['school'] = $schoolName['school_name'];
            }
            if($res) {
                M('UserInfo')->where(array('user_id' => $data['user_id']))->save($data);
            } else {
                $data['user_id'] = $userId;
                M('UserInfo')->add($data);
            }

        } else {
            $this->error('参数错误~');
        }

    }

    /**
     * 获得企业账号信息
     * @param int $accountId
     * @param string $field
     */
    public function getAccountInfo($accountId,$field='*')
    {
        $condition = array(
            'account_id' => $accountId,
            'cut_off' => 0
        );
        $data = D('Account')->where($condition)->field($field)->find();

        return $data;
    }

    /**
     * 获得用户信息
     * @param array $condition 查询条件
     * @param string $field 字段列表
     * @return mixed
     */
    public function getUserInfo($condition,$field='*')
    {
        $data = D('User')->getInfo($condition,$field);
        return $data;
    }


    /**
     * 访问杭师大认证接口
     * @param string $accountEnName 企业账号英文名稱
     * @param array $postParams 从客户端提交过来的参数
     */
    public function callAouthApi($accountEnName,$postParams)
    {
        if (!$accountEnName) {
            throw_exception('企业账号英文名称不能为空！');
        }

        //加载类
        $accountEnName = ucfirst($accountEnName);
        $className = "{$accountEnName}OauthInterf";
        import('@.Interface.Base.'.$className);

        if ( !class_exists($className) ) {
            throw_exception('接口访问对象不存在！');
        }

        $userOauthInterf = new $className();
        if (!method_exists($userOauthInterf,'callOauthIntf')) {
            throw_exception('接口访问方法不存在！');
        }

        //在调用callOauthIntf之前,先要调用setProperties方法来设置对象属性
        $res = $userOauthInterf->setProperties($postParams)->callOauthIntf();
        return $res;
    }
}